package shuashuami.hb.com.fragment.agent;

import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.CSubordinateAdatper;
import shuashuami.hb.com.avtivity.AbActivity;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.model.Csubordinate;

/**
 * created by zhangqi at 2017/7/11
 */
public class SubordinateAct extends AbActivity {

    private ListView listView;

    private CSubordinateAdatper adatper;
    private List<Csubordinate> list;

    private int page = 1;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.act_subordinate);
        listView = (ListView) findViewById(R.id.lv_csubordinate_items);
    }

    @Override
    protected void initData() {
        setTitleBar("我的刷手");
        setLeftView();
        recommendListc();
    }

    @Override
    protected void initListener() {

    }

    private void initAdapter(JSONArray jsonArray) {
        list = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject temp = jsonArray.getJSONObject(i);
                Csubordinate csubordinate = new Csubordinate(temp.getInt("id"), temp.getString("username"),
                        temp.getString("avatar"), temp.getString("mobile"), temp.getInt("status"));
                list.add(csubordinate);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        adatper = new CSubordinateAdatper(list, this);
        listView.setAdapter(adatper);
    }

    private void recommendListc() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
//        ShareAUserInfo shareCUserInfo = new ShareAUserInfo(this);
        HttpAgentMethods.recommendListc(listener, page + "", "10"
        );
//        HttpMethods.recommendListc(listener, shareCUserInfo.getId(), page + "", "10"
//        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(SubordinateAct.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) { //成功
                            JSONObject temp = jsonObject.getJSONObject("content");
                            JSONArray jsonArray = temp.getJSONArray("list");
                            initAdapter(jsonArray);
                        } else {
                            ToastUtil.showShort(SubordinateAct.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(SubordinateAct.this, "数据解析错误");
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
